<?php

namespace Usuarios\Model;

//use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Predicate\Expression;

require_once('nusoap/nusoap.php');
use nusoap_client as SoapClient;

class AlumnoTable {

/*   protected $tableGateway;

   public function __construct(TableGateway $tableGateway) {
      $this->tableGateway = $tableGateway;
   }*/

   private $client;

   public function __construct() {
      $this->client = new SoapClient('http://soap.asistencia.uca.edu.py/usuarios_soap.php');
   }

/*   public function fetchAll() {
      $resultSet = $this->tableGateway->select();
      return $resultSet;
   }*/

   //SOAP Client Version
   public function fetchAll() {

      $result = $this->client->call('alumnotable_fetchAll',array());
      //var_dump($result);

      //convertimos la respuesta de array a objetos
      $rowset = json_decode(json_encode($result), FALSE);

      return $rowset;
   }

/*   public function deleteMissingIDs() {
      $select  = new \Zend\Db\Sql\Select;
//SELECT A.matricula FROM alumnos A LEFT JOIN usuarios U ON A.matricula = U.matricula WHERE U.matricula is null;
      $select->from('alumnos');
      $select->columns(array('matricula'));
      $select->join('usuarios',"usuarios.matricula = alumnos.matricula",array('m' => 'matricula'),'left');
      $select->where('usuarios.matricula is null');
      //echo $select->getSqlString();

      $rowset = $this->tableGateway->selectWith($select);
      //var_dump($rowset);
      //echo "<br>" . $rowset->count() . "<br>";
      //echo "<br>";
      foreach($rowset as $row) {
         //echo $row->matricula; echo "<br>";
         $this->deleteAlumno($row->matricula);
         // var_dump($row);
      }
   }*/

   //SOAP Client Version
   public function deleteMissingIDs() {

      $result = $this->client->call('alumnotable_getMissingIDs',array());

      //convertimos la respuesta de array a objetos
      $rowset = json_decode(json_encode($result), FALSE);
      foreach($rowset as $row) {
         $this->deleteAlumno($row->matricula);
      }
   }

/*   public function getAlumno($matricula) {
      $matricula = (int) $matricula;
      $rowset = $this->tableGateway->select(array('matricula' => $matricula));
      $row = $rowset->current();
      if (!$row) {
         throw new \Exception("Could not find row $matricula");
      }
      return $row;
   }*/

   //SOAP Client Version
   public function getAlumno($matricula) {
      $matricula = (int) $matricula;

      $result = $this->client->call('getAlumno',array('matricula' => $matricula));

      if (empty($result)) {
         throw new \Exception("Could not find row $matricula");
      }
      $rowset = json_decode(json_encode($result), FALSE);
      return $rowset[0];
   }

   /*public function getlastAlumno() {
      $select = $this->tableGateway->getSql()->select();
      $a = 0;
      $select->where('matricula > '. $a)
             ->order('matricula DESC')
             ->limit(1);
      $rowset = $this->tableGateway->selectWith($select);
      $row = $rowset->current();
      if (!$row) {
         throw new \Exception("Could not find last Alumno");
      }
      return $row;
   }*/

   //SOAP Client Version
   public function getlastAlumno() {

      $result = $this->client->call('getlastAlumno', array());

      if (empty($result)) {
         throw new \Exception("Could not find last Alumno");
      }
      $rowset = json_decode(json_encode($result), FALSE);
      return $rowset[0];
   }

/*   public function saveAlumno(Alumno $alumno) {
      $data = array(
         'fecha_ingreso' => $alumno->fecha_ingreso,
      );

      $matricula = (int)$alumno->matricula;
      if ($matricula == 0) {
         if (is_null($data['fecha_ingreso']))
            $data['fecha_ingreso'] = new Expression('default');
         $this->tableGateway->insert($data);
      } else {
         if ($this->getAlumno($matricula)) {
            $this->tableGateway->update($data, array('matricula' => $matricula));
         } else {
            throw new \Exception('matricula does not exist');
         }
      }

   }*/

   //SOAP Client Version
   public function saveAlumno(Alumno $alumno) {

      $matricula = (int)$alumno->matricula;
      if ($matricula == 0) {
         $this->client->call('insertAlumno', array());
      } else {
         if ($this->getAlumno($matricula)) {
            $this->client->call('updateAlumno', array('fecha_ingreso' => $alumno->fecha_ingreso));
         } else {
            throw new \Exception('matricula does not exist');
         }
      }
   }


/*   public function deleteAlumno($matricula) {
      $this->tableGateway->delete(array('matricula' => $matricula));
   }*/

   //SOAP Client version
   public function deleteAlumno($matricula) {
      $this->client->call('deleteAlumno', array('matricula' => $matricula));
   }

}
